package com.zjitc.service.imp;

import com.github.pagehelper.PageHelper;
import com.zjitc.dao.OrderDao;
import com.zjitc.dao.ProductDao;
import com.zjitc.model.Order;
import com.zjitc.model.OrderItem;
import com.zjitc.model.Product;
import com.zjitc.service.OrderService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/15 17:02
 * @description:
 */
@Service
public class OrderServiceImp implements OrderService{
  private static final Integer SIZE=3;
  @Autowired
  public OrderDao orderDao;

  @Autowired
  public ProductDao productDao;

  @Override
  public void add(Order order) {
    orderDao.add(order);
    for(OrderItem orderItem:order.getItems()){
      orderDao.addItems(orderItem);
    }
  }

  @Override
  public List<Order> findByUid(String uid, Integer pageNum) {
    PageHelper.startPage(pageNum,SIZE);
    List<Order> orders=orderDao.findByUid(uid);
    for(Order order :orders){
      List<OrderItem> list=orderDao.findItems(order.getOid());
      for(OrderItem orderItem:list){
        Product product=productDao.findByPid(orderItem.getPid());
        orderItem.setSubtotal(product.getShop_price()*orderItem.getCount());
        orderItem.setProduct(product);
      }
      order.setItems(list);
    }
    return orders;
  }

  @Override
  public void delete(String oid) {
    orderDao.deleteItems(oid);
    orderDao.delete(oid);
  }

  @Override
  public Order findByOid(String oid) {
    Order order=orderDao.findByOid(oid);
    List<OrderItem> orderItem=orderDao.findOrderItem(oid);
    for(OrderItem o:orderItem){
      o.setProduct(productDao.findByPid(o.getPid()));
    }
    order.setItems(orderItem);
    return order;
  }

  @Override
  public void payForUpdateOid(String oid) {
    orderDao.updateOid(oid);
  }

  @Override
  public List<Order> findAllOrders(Integer pageNum, Integer count) {
    PageHelper.startPage(pageNum,count);
    List<Order> orders=orderDao.findAllOrders();
    return orders;
  }

  @Override
  public List<Order> findAllByState(Integer state, Integer pageNum, Integer count) {
    PageHelper.startPage(pageNum,count);
    List<Order> orders=orderDao.findAllByState(state);
    return  orders;
  }

  @Override
  public Order findOid(String oid) {
    return orderDao.findByOid(oid);
  }

  @Override
  public void updateRefund(String o) {
    orderDao.updateRefund(o);
  }

  @Override
  public void updateSend(String oid, String express, Integer state) {
    orderDao.updateSend(oid,express,state);
  }

  @Override
  public List<OrderItem> findByPid(String pid) {
    return orderDao.findByPid(pid);
  }

  @Override
  public void deletePid(String pid) {
    orderDao.deletePid(pid);
  }


}
